package com.zbxx.service.impl;

import com.zbxx.dao.EmployeeDao;
import com.zbxx.domain.Employee;
import com.zbxx.domain.PageBean;
import com.zbxx.service.EmployeeService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
@Transactional
public class EmployeeServiceImpl implements EmployeeService {
    //注入dao
    @Resource
    private EmployeeDao employeeDao;

    /*
     * 业务层登录的方法
     * */
    @Override
    public Employee login(Employee employee) {
        Employee existEmployee = employeeDao.findByUsernameAndPassword(employee);
        return existEmployee;
    }

    /*业务层分页查询员工的方法*/
    @Override
    public PageBean<Employee> findByPage(Integer currPage) {
    PageBean<Employee> pageBean =new PageBean<Employee>();
    //封装数据
        pageBean.setCurrPage(currPage);
        //封装每页显示的记录数
        int pageSize=3;
        pageBean.setPageSize(pageSize);
        //封装总的记录数
        int totalCount=employeeDao.findCount();
        pageBean.setTotalCount(totalCount);
        //封装总页数
        double tc=totalCount;
        Double num=Math.ceil(tc/pageSize);
        pageBean.setTotalPage(num.intValue());
        //封装每页所显示的数据
        int begin=(currPage-1)* pageSize;
        List<Employee> list=employeeDao.findByPage(begin,pageSize);
        pageBean.setList(list);
        return pageBean;
    }

    //业务层保存员工的方法
    @Override
    public void save(Employee employee) {
        employeeDao.save(employee);
    }

    //业务层根据id查询员工
    @Override
    public Employee findById(Integer eid) {
        return employeeDao.finById(eid);
    }

    //业务层修改员工的方法
    @Override
    public void update(Employee employee) {
        employeeDao.update(employee);
    }
    //业务层删除员工的方法
    @Override
    public void delete(Employee employee) {
        employeeDao.delete(employee);
    }


    public void setEmployeeDao(EmployeeDao employeeDao) {
        this.employeeDao = employeeDao;
    }

    public EmployeeDao getEmployeeDao() {
        return employeeDao;
    }
}
